package cn.mldn.microcloud.controller;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import cn.mldn.microcloud.service.IConsumerRemoteService;
import cn.mldn.vo.Dept;

@RestController
@RefreshScope
public class ConsumerDeptController {
	public static final String DEPT_GET_URL = "http://localhost:8001/dept/get/";
	public static final String DEPT_LIST_URL = "http://localhost:8001/dept/list/";
	public static final String DEPT_ADD_URL = "http://localhost:8001/dept/add?dname=";
	@Resource
	private RestTemplate restTemplate;
	@Resource
	private HttpHeaders headers;
	@Resource
	IConsumerRemoteService iConsumerRemoteService;
	
/*	@Value("${author}")
    private String author;*/
	
	@RequestMapping(value = "/consumer/dept/get")
	public Object getDept(long id) {
		Dept dept = this.restTemplate
				.exchange(DEPT_GET_URL + id, HttpMethod.GET,
						new HttpEntity<Object>(this.headers), Dept.class)
				.getBody();
		iConsumerRemoteService.get(id);
		return dept;
	}
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/consumer/dept/list")
	public Object listDept() {
		Object allDepts = iConsumerRemoteService.list();
		/*List<Dept> allDepts = this.restTemplate
				.exchange(DEPT_LIST_URL, HttpMethod.GET,
						new HttpEntity<Object>(this.headers), List.class)
				.getBody();*/
		return allDepts;
	}
	@RequestMapping(value = "/consumer/dept/add")
	public Object addDept(Dept dept) throws Exception {
		Boolean flag = this.restTemplate.exchange(DEPT_ADD_URL, HttpMethod.POST,
				new HttpEntity<Object>(dept, this.headers), Boolean.class)
				.getBody();
		return flag;
	}
	
/*    @RequestMapping(value = "/author")
    public String author(){
        return author;
    }*/
}
